package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BWarehouseService;
import cn.huiyunche.base.service.mappers.BWarehouseMapper;
import cn.huiyunche.base.service.model.BWarehouse;
import cn.huiyunche.base.service.model.BWarehouseExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.tms.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/2 上午11:45
 */
@Service
public class BWarehouseServiceImpl implements BWarehouseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BWarehouseServiceImpl.class);

    @Autowired
    private BWarehouseMapper bWarehouseMapper;

    @Override
    public Integer getIdByConditions(String warehouseCode, String warehouseName, String warehouseAddress, String warehouseProvinceName, String warehouseCityName) {
        LOGGER.info("getIdByConditions params : {}, {}, {}, {}, {}", warehouseCode, warehouseName, warehouseAddress, warehouseProvinceName, warehouseCityName);
        if (StringUtils.isBlank(warehouseCode)) {
            LOGGER.error("getIdByConditions param warehouseCode must not be null");
            throw new IllegalArgumentException("仓库编号不能为空");
        }
        if (StringUtils.isBlank(warehouseName)) {
            LOGGER.error("getIdByConditions param warehouseName must not be null");
            throw new IllegalArgumentException("仓库名称不能为空");
        }
//        if(StringUtils.isBlank(warehouseAddress)){
//            LOGGER.error("getIdByConditions param warehouseAddress must not be null");
//            throw new IllegalArgumentException("仓库地址不能为空");
//        }
        if (StringUtils.isBlank(warehouseProvinceName)) {
            LOGGER.error("getIdByConditions param warehouseProvinceName must not be null");
            throw new IllegalArgumentException("仓库所在省不能为空");
        }
        if (StringUtils.isBlank(warehouseCityName)) {
            LOGGER.error("getIdByConditions param warehouseCityName must not be null");
            throw new IllegalArgumentException("仓库所在市不能为空");
        }

        BWarehouseExample example1 = new BWarehouseExample();
        BWarehouseExample.Criteria criteria = example1.createCriteria().andWarehouseCodeEqualTo(warehouseCode).andWarehouseNameEqualTo(warehouseName).andProvinceNameEqualTo(warehouseProvinceName).andCityNameEqualTo(warehouseCityName);
        if (StringUtils.isNotBlank(warehouseAddress)) {
            criteria.andAddressEqualTo(warehouseAddress);
        }

        BWarehouseExample example2 = new BWarehouseExample();
        example2.createCriteria().andWarehouseCodeEqualTo(warehouseCode);

        List<BWarehouse> list = bWarehouseMapper.selectByExample(example1);
        if (CollectionUtils.isEmpty(list)) {
            list = bWarehouseMapper.selectByExample(example2);
            if (CollectionUtils.isNotEmpty(list)) {
                BWarehouse bWarehouse = list.get(0);
                bWarehouse.setWarehouseName(warehouseName);
                if (StringUtils.isNotBlank(warehouseAddress)) {
                    bWarehouse.setAddress(warehouseAddress);
                }
                bWarehouse.setProvinceName(warehouseProvinceName);
                bWarehouse.setCityName(warehouseCityName);
                bWarehouseMapper.updateByPrimaryKeySelective(bWarehouse);
                return bWarehouse.getId();
            }

        } else {
            return list.get(0).getId();
        }

        BWarehouse bWarehouse = new BWarehouse();
        bWarehouse.setWarehouseCode(warehouseCode);
        bWarehouse.setWarehouseName(warehouseName);
        if (StringUtils.isNotBlank(warehouseAddress)) {
            bWarehouse.setAddress(warehouseAddress);
        }
        bWarehouse.setProvinceName(warehouseProvinceName);
        bWarehouse.setCityName(warehouseCityName);
        bWarehouseMapper.insertSelective(bWarehouse);

        return bWarehouse.getId();
    }
}
